//tag::include[]
package com.lianyou.chapter03.crossparameter;

//end::include[]
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.executable.ExecutableValidator;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

//tag::include[]
public class Car {

	@LuggageCountMatchesPassengerCount(piecesOfLuggagePerPassenger = 2)
	public void load(List<Person> passengers, List<PieceOfLuggage> luggage) {
		//...
	}


	@Test
	public void test1 () throws NoSuchMethodException {
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		ExecutableValidator executableValidator = factory.getValidator().forExecutables();
		Validator validator = factory.getValidator();

		Car car = new Car();

		Method load = car.getClass().getMethod("load", List.class, List.class);

		Person person = new Person();
		PieceOfLuggage pieceOfLuggage = new PieceOfLuggage();


		Set<ConstraintViolation<Car>> constraintViolations = executableValidator.validateParameters(car, load, new Object[]{Arrays.asList(person), Arrays.asList(pieceOfLuggage, pieceOfLuggage)});
		Assertions.assertEquals(1,constraintViolations.size(),"有异常");
		ConstraintViolation<Car> next = constraintViolations.iterator().next();
		System.out.println(next.getMessage());
		System.out.println(next.getPropertyPath());

	}
}
//end::include[]
